﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ChinaSoft.BLL;
using ChinaSoft.Model;
using System.Drawing;

namespace MidSoft.Sys
{
    public partial class SetUserPass : System.Web.UI.Page
    {
        public string strid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["currentUserID"] != null && Session["currentUserID"].ToString().Trim() != "")
                {
                    strid = Session["currentUserID"].ToString();
                    int UsersId = (Convert.ToInt32(strid));
                    ShowInfo(UsersId);
                }
            }
        }

        private void ShowInfo(int UsersId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            ChinaSoft.Model.Model_tb_MidSoft_Users model = bll.GetModel(UsersId);
            this.lblName.Text = model.UserAccount;
        }
        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void btnUpdate_Click(object sender, System.EventArgs e)
        {
            string username = this.lblName.Text;
            string passward = Maticsoft.Common.DEncrypt.DEncrypt.MD5(this.txtOldPassword.Text.Trim(), 16);

            BLL_tb_MidSoft_Users bllUsers = new BLL_tb_MidSoft_Users();
            Model_tb_MidSoft_Users modelUsers = new Model_tb_MidSoft_Users();
            List<Model_tb_MidSoft_Users> listUsers = bllUsers.GetModelList("UserAccount='" + username + "' and UserPassword='" + passward + "'");
            if (listUsers != null && listUsers.Count > 0 && listUsers[0].UsersId > 0)
            {
                listUsers[0].UserPassword = Maticsoft.Common.DEncrypt.DEncrypt.MD5(this.txtPassword.Text.Trim(), 16);
                if (bllUsers.Update(listUsers[0]))
                {
                    this.lblMsg.ForeColor = Color.Blue;
                    this.lblMsg.Text = "用户信息更新成功！";
                    #region 添加日志
                    ChinaSoft.Model.Model_tb_MidSoft_log modelLog = new ChinaSoft.Model.Model_tb_MidSoft_log();
                    ChinaSoft.BLL.BLL_tb_MidSoft_log bllLog = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
                    modelLog.userAccount = "";
                    if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                        modelLog.userAccount = Session["UserAccount"].ToString();
                    modelLog.userName = "";
                    if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                        modelLog.userName = Session["UserName"].ToString();
                    modelLog.description = "修改密码,UserAccount=" + username;
                    modelLog.LogType = 2;
                    modelLog.CreateTime = DateTime.Now;
                    bllLog.Add(modelLog);
                    #endregion
                }
                else
                {
                    this.lblMsg.ForeColor = Color.Red;
                    this.lblMsg.Text = "更新用户信息发生错误！";
                }
            }
            else
            {
                this.lblMsg.ForeColor = Color.Red;
                this.lblMsg.Text = "原始密码有误！";
            }
        }
    }
}